package com.threeti.mecool.legacy.domain.model.basicdata;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.threeti.mecool.shared.util.AbstractFlatRawTreeNode;

/** 
 * 国家行政区划(遗留)
 * 
 * 通过乡镇区划编号将国标的行政区划体系扩展到乡镇级别
 * 业务主键：国家行政区划代码（国标）+乡镇区划编号（默认00，自增）';
 * 
 * 36250100 疑似遗留数据非法数据(未找到正确的市辖区节点)
 * 37140100 疑似遗留数据非法数据(未找到正确的市辖区节点)
 * 50030000 重庆市(市)，(市)是比较奇怪的一种地域类型，可能需要额外之判断
 * 
 * @author Jay Meng
 */
@Entity
@Table(name = "MD_ADMIN_DIV")
public class AdminDivisionLegacy extends AbstractFlatRawTreeNode<String> {
  @Column(name="AD_ID")
  private Long id;//jay:degrade to candidate business key
  
  @Id
  @Column(name="AD_CODE")
  private String code;
  
  @Column(name="AD_NAME")
  private String name;
  
  @Column(name="CITY_AD_NAME")
  private String parentCityAdName;
  
  @Column(name="PROVINCE_AD_NAME")
  private String parentProvinceAdName;
  
  @Column(name="DEEP")
  private int deep;

  @Override
  public String getId() {
    return code;
  }
  
  /** 通过国家标准的编码规则推断出当前地域的上级(省、市、区、县)编码 */
  @Override
  public String getParentId() {
    if (deep == 1) {
      return null;
    } else if (deep == 2) {
      return code.substring(0, 2) + "000000";
    } else if (deep == 3) {
      return code.substring(0, 4) + "0000";
    } else {
      return code.substring(0, 6) + "00";
    }
  }

  @Override
  public String getName() {
    return name == null ? "" : name;
  }

  public String getParentCityAdName() {
    return parentCityAdName == null ? "" : parentCityAdName;
  }

  public String getParentProvinceAdName() {
    return parentProvinceAdName == null ? "" : parentProvinceAdName;
  }

  public String getCode() {
    return code;
  }

  public int getDeep() {
    return deep;
  }

  @Override
  public String toString() {
    return "AdminDivisionLegacy [id=" + id + ", code=" + code + ", name=" + name + ", parentCityAdName="
        + parentCityAdName + ", parentProvinceAdName=" + parentProvinceAdName + ", deep=" + deep + "]";
  }
  
  public Long getAdId(){
	  return id;
  }
}
